package com.san.hkhospital.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.san.hkhospital.entity.Payin;
import com.san.hkhospital.service.PayinService;
import com.san.hkhospital.service.impl.PayinServiceImpl;

import com.san.hkhospital.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* (payin)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("payin")
public class PayinController {
/**
* 服务对象
*/
@Resource
private PayinServiceImpl payinServiceImpl;
@Autowired
private PayinService payinService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
//@GetMapping("selectOne")
//public Payin selectOne(Integer id) {
//return payinServiceImpl.selectByPrimaryKey(id);
//}
//


@GetMapping("payInList")
public ResultVO payInList(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize){
    PageHelper.startPage(pageNum,pageSize);
    List<Payin> payInList = payinService.payInList();
    PageInfo<Payin> pageInfo = new PageInfo<Payin>(payInList);
    return ResultVO.success("所有收入查看成功",pageInfo);
}

    @GetMapping("/searchPayIn")
    public ResultVO searchPayIn(
            @RequestParam Map<String, String> params,
            @RequestParam(value = "startDate", required = false) String startDateStr,
            @RequestParam(value = "endDate", required = false) String endDateStr,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize
    ) {
        try {
            // 手动转换日期参数
            if (startDateStr != null && endDateStr != null) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime startDate = LocalDateTime.parse(startDateStr, formatter);
                LocalDateTime endDate = LocalDateTime.parse(endDateStr, formatter);
                params.put("startDate", startDate.toString());
                params.put("endDate", endDate.toString());
            }

            PageHelper.startPage(pageNum, pageSize);
            List<Payin> searchList = payinService.searchList(params);
            PageInfo<Payin> pageInfo = new PageInfo<>(searchList);
            return ResultVO.success("搜索查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failed("搜索查询失败");
        }
    }


@PostMapping("addPayIn")
    public ResultVO addPayIn(@RequestBody Payin payin){
    System.out.println("Received Payin: " + payin);
    boolean flag = payinService.addPayIn(payin);
    if(flag){
        return ResultVO.success("收入记录添加成功");
    }
    return  ResultVO.failed("收入记录添加失败");
}



@PostMapping("/updatePayIn/{id}")
    public ResultVO updatePayIn(@PathVariable int id, @RequestBody Payin payin){
    payin.setInId(id);
   int flag = payinService.updatePayIn(payin);
    if(flag==0){
        return ResultVO.failed("收入记录修改失败");
    }
    return  ResultVO.success("收入记录修改成功");
}






}
